package com.example.knowing_project.controller;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.knowing_project.common.BaseResponse;
import com.example.knowing_project.common.ResultUtils;
import com.example.knowing_project.exception.BusinessException;
import com.example.knowing_project.pojo.User;
import com.example.knowing_project.pojo.req.UserLoginReq;
import com.example.knowing_project.pojo.req.UserQueryReq;
import com.example.knowing_project.pojo.req.UserResetPasswordReq;
import com.example.knowing_project.pojo.req.UserSaveReq;
import com.example.knowing_project.pojo.resp.UserLoginResp;
import com.example.knowing_project.service.UserService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import static com.example.knowing_project.common.ErrorCode.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private RedisTemplate redisTemplate;

    @PostMapping("/list")
    public BaseResponse list(@RequestBody UserQueryReq req) {

        return ResultUtils.success(userService.listUser(req));

    }

    @PostMapping("/save")
    public BaseResponse save(@RequestBody UserSaveReq req) {

        User user = new User();
        String s = SecureUtil.md5(req.getPassword());
        user.setLogin_name(req.getLoginName());
        user.setName(req.getName());
        user.setPassword(s);

        userService.save(user);
        return ResultUtils.success(null);


    }

    @PostMapping("/delete/{id}")
    public BaseResponse delete(@PathVariable Long id) {
        boolean flog = userService.removeById(id);
        if (!flog) {
            throw new BusinessException(OPERATION_ERROR);
        }
        return ResultUtils.success(null);

    }

    @PostMapping("/reset-password")
    public BaseResponse resetPassword(@RequestBody UserResetPasswordReq req) {
        Long id = req.getId();
        User user = userService.getById(id);
        if (user == null) {
            return ResultUtils.error(NOT_FOUND_ERROR);
        }
        String s = SecureUtil.md5(req.getPassword());
        User user1 = new User();
        user1.setId(id);


        user1.setPassword(SecureUtil.md5(req.getPassword()));

        boolean b = userService.updateById(user1);
        if (!b) {
            return ResultUtils.error(NOT_FOUND_ERROR);
        }
        return ResultUtils.success(SUCCESS);
    }

    @PostMapping("/login")
    public BaseResponse login(@RequestBody UserLoginReq req) {
        String s = SecureUtil.md5(req.getPassword());
        String loginName = req.getLoginName();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("login_name", loginName);
        User one = userService.getOne(queryWrapper);
        if (one == null) {
            return ResultUtils.error(NOT_FOUND_ERROR);
        }
        QueryWrapper<User> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.ge("login_name", loginName).ge("password", s);
        User one1 = userService.getOne(queryWrapper1);
        if (one1 == null) {
            return ResultUtils.error(NOT_FOUND_ERROR);
        }

        UserLoginResp userLoginResp = new UserLoginResp();

        userLoginResp.setLoginName(one1.getLogin_name());
        userLoginResp.setName(one1.getName());

        String token = UUID.randomUUID().toString();

        userLoginResp.setToken(token);
        redisTemplate.opsForValue().set(token, JSONUtil.toJsonStr(userLoginResp), 1L, TimeUnit.DAYS);

        return ResultUtils.success(userLoginResp);
    }

    @GetMapping("/logout/{token}")
    public BaseResponse logout(@PathVariable String token) {

        redisTemplate.delete(token);
        return ResultUtils.success(null);
    }

    public static void main(String[] args) {
        String s = SecureUtil.md5(123456+"");
        System.out.println(s);
    }
}
